package com.zqxq.lottery.cms.controller;

import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.cms.feign.LotteryServerFeignClient;
import com.zqxq.lottery.common.utils.ExcelUtils;
import com.zqxq.lottery.entity.vo.user.UserAddressVO;
import com.zqxq.lottery.entity.vo.user.UserProductTradExcelVO;
import io.swagger.annotations.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 苦智
 * @date 2018/9/10 10:55
 * @description 交易管理
 * @ClassName TradManageController
 */
@RestController
@Api(value = "交易管理",tags = "交易管理")
@Slf4j
@RequestMapping(value = "/trade",produces = "application/json")
public class TradManageController {
    @Autowired
    private LotteryServerFeignClient lotteryServerFeignClient;

    @PostMapping("/list")
    @ApiOperation("兑换列表")
    public Result userProductList(@ApiParam(value = "页码",required = true) @RequestParam(defaultValue = "1") String pageNO,
                                  @ApiParam(value = "页数",required = true) @RequestParam(defaultValue = "10") String pageSize,
                                  @ApiParam(value = "开始时间",required = false) String startTime,
                                  @ApiParam(value = "结束时间",required = false) String endTime,
                                  @ApiParam(value = "兑换方式 1：兑换实物 2：代卖",required = false) String tradeType,
                                  @ApiParam(value = "处理状态 0:未处理 1:已处理",required = false) String status,
                                  @ApiParam(value = "电话",required = false) String mobile,
                                  @ApiParam(value = "姓名",required = false) String name){
        if (pageNO == null || pageSize == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),"pageNO或者pageSize为空");
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("pageNO",pageNO);
        paramMap.put("pageSize",pageSize);
        paramMap.put("startTime",startTime);
        paramMap.put("endTime",endTime);
        paramMap.put("tradeType",tradeType);
        paramMap.put("status",status);
        paramMap.put("mobile",mobile);
        paramMap.put("name",name);
        return lotteryServerFeignClient.getUserProductPage(paramMap);
    }

    @PostMapping("/export")
    @ApiOperation("兑换列表导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime",value = "开始时间",required = false,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "endTime",value = "结束时间",required = false,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "tradType",value = "兑换方式 1：兑换实物 2：代卖",required = false,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "status",value = "处理状态 0:未处理 1:已处理",required = false,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "mobile",value = "手机号",required = false,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "name",value = "姓名",required = false,paramType = "query",dataType = "String")
    })
    public Result exportUserProductList(String startTime, String endTime, Integer tradeType, Integer status, Integer mobile, String name,
                                        HttpServletResponse response){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("startTime",startTime);
        paramMap.put("endTime",endTime);
        paramMap.put("tradeType",tradeType);
        paramMap.put("status",status);
        paramMap.put("mobile",mobile);
        paramMap.put("name",name);
        Result<List<UserProductTradExcelVO>> r = lotteryServerFeignClient.getUserProductList(paramMap);
        if (r.success()){
            List<UserProductTradExcelVO> tradVOList = r.getData();
            if (!ExcelUtils.downloadExcel(response,tradVOList,UserProductTradExcelVO.class,"兑换订单","兑换订单")){
                return new Result(CodeEnum.FAIL.getCode(),CodeEnum.FAIL.getMsg());
            }
        }
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg());
    }

    @PostMapping("/detail")
    @ApiOperation("兑换详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeId",value = "交易id",required = true,paramType = "query",dataType = "String")
    })
    public Result userProductDetail(String tradeId){
        if (StrUtils.isEmpty(tradeId))
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        return lotteryServerFeignClient.getUserProductDetail(tradeId);
    }

    /**
    * @Author 苦智
    * @Description 发货
    * @Date 17:13 2018/9/10
    * @Param [tradeId]
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/delivery")
    @ApiOperation("发货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeId",value = "交易id",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "logisticsNum",value = "物流单号",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "logisticsCompany",value = "物流公司",required = true,paramType = "query",dataType = "String")
    })
    public Result deliveryToUser(String tradeId,String logisticsNum,String logisticsCompany){
        if (StrUtils.isEmpty(tradeId) || StrUtils.isEmpty(logisticsNum) || StrUtils.isEmpty(logisticsCompany))
            return new Result(CodeEnum.FAIL_100002.getCode(),"交易id不能为空");
        return lotteryServerFeignClient.deliveryToUser(tradeId,logisticsNum,logisticsCompany);
    }

    /**
    * @Author 苦智
    * @Description 修改收货信息
    * @Date 17:52 2018/9/10
    * @Param [tradeId, name]
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/editReceivingInfo")
    @ApiOperation("修改收货信息")
    public Result updateReceivingInfo(UserAddressVO userAddressVO){
        if (userAddressVO == null || StrUtils.isEmpty(userAddressVO.getProvince()) ||
                StrUtils.isEmpty(userAddressVO.getCity()) || StrUtils.isEmpty(userAddressVO.getCounty()) ||
                StrUtils.isEmpty(userAddressVO.getStreet()) || StrUtils.isEmpty(userAddressVO.getDetailAddress()) ||
                StrUtils.isEmpty(userAddressVO.getUserId()))
            return new Result(CodeEnum.FAIL_100002.getCode(),"地址参数不能为空");
        return lotteryServerFeignClient.updateReceivingInfo(userAddressVO);
    }

    @PostMapping("/cancelUserProduct")
    @ApiOperation("取消兑换订单")
    public Result cancelUserProduct(String id){
        if ( StrUtils.isEmpty(id)) {
            return new Result(CodeEnum.FAIL_100002.getCode(), "订单编号不能为空");
        }
        return lotteryServerFeignClient.cancelUserProduct(id);
    }
}
